// mircoApp/cloud_shop/billing_Zone/billing_Zone.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: [],
    list: [],
    page: 1,
    total_page: 0,
    isLoadMore: true,
    self_reward_name: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync("langIndex");
    let yz_basic_info = wx.getStorageSync("yz_basic_info");
    console.log(yz_basic_info.plugin_setting.cloud_shop);
    // let basic_info =
    this.setData({
      language: language.en,
      self_reward_name: yz_basic_info.plugin_setting.cloud_shop.self_reward_name,
    });
    this.init();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  toGoodDetail(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + item.id,
    });
  },
  toShare(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/mircoApp/cloud_shop/cloud_shop_share/cloud_shop_share?brand_id=${item.brand_id}`,
    });
  },
  randomShare() {
    let lis = this.data.list.filter((item) => item.brand_id != 0);
    let randomIndex = Math.floor(Math.random() * lis.length);
    let brand_id = this.data.list[randomIndex].brand_id;
    wx.navigateTo({
      url: `/mircoApp/cloud_shop/cloud_shop_share/cloud_shop_share?brand_id=${brand_id}`,
    });
  },
  init() {
    this.setData({
      page: 1,
      list: [],
      isLoadMore: true,
      total_page: 0,
    });
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.kaidan.index"),
    });
    if (data.result) {
      this.setData({
        info: data.data,
        list: data.data.list.data,
        total_page: data.data.last_page,
        isLoadMore: true,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      });
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.kaidan.index"),
        data: {
          page: this.data.page,
        },
      });
      if (data.result) {
        this.setData({
          list: this.data.list.concat(data.data.list.data),
          isLoadMore: true
        });
      } else {
        app.tips(data.msg);
        this.setData({
          page: this.data.page - 1,
        });
      }
    }
  },
});